package com.nsqt.banquan.controller;

import com.nsqt.banquan.domain.MailListDO;
import com.nsqt.banquan.service.MailListService;
import com.nsqt.banquan.service.WorkService;
import com.nsqt.common.utils.PageUtils;
import com.nsqt.common.utils.Query;
import com.nsqt.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 证书邮寄
 */
@Controller
@RequestMapping("/banquan/maillist")
public class MailListController {
    @Autowired
    private MailListService mailListService;
    @Autowired
    private WorkService workService;

    @GetMapping()
    @RequiresPermissions("banquan:maillist:maillist")
    String Maillist() {
        return "banquan/maillist/maillist";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("banquan:maillist:maillist")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<MailListDO> maillistList = mailListService.list(query);
        int total = mailListService.count(query);
        PageUtils pageUtils = new PageUtils(maillistList, total);
        return pageUtils;
    }

    /**
     * 新增证书邮寄
     */
    @GetMapping("/add")
    @RequiresPermissions("banquan:maillist:add")
    String add() {

        return "banquan/maillist/add";
    }

    /**
     * 新增证书邮寄
     */
    @GetMapping("/mail")
    @RequiresPermissions("banquan:maillist:add")
    String mailLicenses(@RequestParam("sns") String sns, Model model) {
        String copyrightSn = "";
        String[] snArr = sns.split(",");
        for (int a = 0; a < snArr.length; a++) {
            String sn = snArr[a];
            if (sn != null && sn != "") {
                copyrightSn += (a + 1) + "." + workService.getBySn(sn).getSoftwareName() + ",\n";
            }
        }

        model.addAttribute("copyrightSn", copyrightSn);
        model.addAttribute("sns", sns);
        return "banquan/maillist/add";
    }

    /**
     * 编辑证书邮寄
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("banquan:maillist:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        MailListDO maillist = mailListService.get(id);
        model.addAttribute("maillist", maillist);
        return "banquan/maillist/edit";
    }

    /**
     * 保存证书邮寄
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("banquan:maillist:add")
    public R save(MailListDO maillist) {
        if (mailListService.save(maillist) > 0) {
            String sns = maillist.getSns();
            String[] snArr = sns.split(",");
            for (String sn : snArr
            ) {
                if (sn != null && sn != "") {
                    workService.mail(sn);
                }
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改证书邮寄
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("banquan:maillist:edit")
    public R update(MailListDO maillist) {
        mailListService.update(maillist);
        return R.ok();
    }

    /**
     * 删除证书邮寄
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("banquan:maillist:remove")
    public R remove(Integer id) {
        if (mailListService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除证书邮寄
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("banquan:maillist:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        mailListService.batchRemove(ids);
        return R.ok();
    }
}
